

. .\Lib.WebPage.ps1


#====================================================================
#
#		TEST FORM WEB APPLICATION
#
#====================================================================


#--------------------------------------------------------------------
Function Get-HtmlTestForm
{
@"
<FORM action="test-form" method="GET">
<P>
	<LABEL for="firstname">First name: </LABEL>
	<INPUT type="text" name="firstname"><BR>
	<LABEL for="lastname">Last name: </LABEL>
	<INPUT type="text" name="lastname"><BR>
	<LABEL for="email">email: </LABEL>
	<INPUT type="text" name="email"><BR>
	<INPUT type="radio" name="sex" value="Male"> Male<BR>
	<INPUT type="radio" name="sex" value="Female"> Female<BR>
	<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
"@
}


#--------------------------------------------------------------------
Function Process-Web-GET
{
	param
	(
		  [psobject] $WebRequest
	)

	[psobject] $WebResponse = New-WebResponse
	$WebResponse.WebRequest		= $WebRequest
	$WebResponse.Resource		= $null
	$WebResponse.ResponseCode	= "501 Not Implemented"

	# Construct the request path.
	[string] $ResourcePath = $WebRequest.WebServer.ApplicationFolder
	[string] $ResourceName = $WebRequest.ResourceName
	[string] $html = ""

	if( $ResourceName -eq "" )
	{
		# Show the test form.
		$html = ""
		$html += Get-HtmlHeader $WebRequest "Test Form" ""
		$html += Get-HtmlTestForm
		$html += Get-HtmlFooter $WebRequest ""
	}
	elseif( $ResourceName -eq "test-form" )
	{
		# Process the test form.
		$html = ""
		$html += Get-HtmlHeader $WebRequest "Test Form" ""
		$html += "Hello "
		if( $WebRequest.Arguments["sex"] -eq "Male" )
			{ $html += "Mr. " }
		elseif( $WebRequest.Arguments["sex"] -eq "Female" )
			{ $html += "Ms. " }
		$html += $WebRequest.Arguments["firstname"] + " " + $WebRequest.Arguments["lastname"] + ".<br>"
		$html += "Would you like to send yourself an <a href=""mailto:$( $WebRequest.Arguments[""email""] )"">email</a>?<br>"
		$html += Get-HtmlFooter $WebRequest ""

	}

	if( $html -ne "" )
	{
		$WebResponse.Resource = New-Resource
		$WebResponse.Resource.Root = ""
		$WebResponse.Resource.Path = $WebRequest.ResourceName
		$WebResponse.Resource.Name = ""
		$WebResponse.Resource.Mime = "text/html"
		$WebResponse.Resource.Data = AsciiString2Bytes( $html )
		$WebResponse.Resource.Size = $WebResponse.Resource.Data.Length
		$WebResponse.ResponseCode = "200 OK"
	}
	else
	{
		$WebResponse.ResponseCode = "404 Not Found"
	}


	if( $WebResponse.Resource -eq $null )
	{
		# Not found
		$WebResponse.ResponseCode = "404 Not Found"
	}
	
	# Send the response.
	[bool] $b = Send-WebResponse $WebResponse $true
	if( $b -eq $false ) { return $false }

	# Return, OK
	return $true
}


